package br.com.felix.fwt.ui;

import java.io.PrintWriter;

import br.com.felix.fwt.servlet.FelixWebToolkitServlet;
import br.com.felix.fwt.ui.exception.ComponentRenderException;

/**
 * The Class Image is used to represent a HTML image tag. Sometimes it is used
 * only to represent src attributes of other tags. For example: Button uses
 * Images objects to know if it will render as input button or input image.
 */
public class Image extends BaseComponent {
	
	private static final long serialVersionUID = -5219470744948976628L;

	public String alt;


	{
		this.cssClass = "fwtimage";
	}
	
	


	/**
	 * Instantiates a new image.
	 */
	public Image() {
	}
	


	/**
	 * Instantiates a new image.
	 * 
	 * @param fileName the path to the image file. Can be absolute or relative.
	 * No checking is made.
	 */
	public Image(String fileName) {
		super(fileName);
	}
	

	/**
     * Instantiates a new image.
     * 
     * @param fileName the path to the image file. Can be absolute or relative.
     * No checking is made.
     * @param alt is the text that will appear as description.
     */
	public Image(String filename, String alt) {
		super(filename);
		this.alt = alt;
	}



	/*
	 * (non-Javadoc)
	 * 
	 * @see br.com.felix.fwt.ui.BaseComponent#write(java.io.PrintWriter)
	 */
	@Override
	public void write(PrintWriter out) throws ComponentRenderException {
		if(!authorized()) return;
		out.write("\n<img class=\"");
		out.write(cssClass);
		out.write("\" ");
		out.write(" name=\"");
		out.write(getName());
		out.write("\"");

		if(alt!=null){
			out.write(" alt=\"");
			out.write(alt);
			out.write("\"");
		}
		out.write(" src=\"");
		out.write(FelixWebToolkitServlet.getFwtContext().request.getContextPath());
		out.write(getValue());
		out.write("\" />");
	}
}
